package com.geek.service.impl;

import com.geek.bean.data.KeyWord;
import com.geek.bean.data.ResponseSearchIndexData;
import com.geek.mapper.MarketKeywordMapper;
import com.geek.mapper.MarketSearchHistoryMapper;
import com.geek.model.MarketKeyword;
import com.geek.model.MarketKeywordExample;
import com.geek.model.MarketSearchHistory;
import com.geek.model.MarketSearchHistoryExample;
import com.geek.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wbw
 * @data 2024/06/17 15:03
 **/
@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    MarketKeywordMapper keywordMapper;
    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;

    @Override
    public ResponseSearchIndexData index() {
        MarketKeyword defaultKey = this.isDefailt();
        List<KeyWord> historyKeyList = this.isHistoryKeyword();
        List<MarketKeyword> hotKeyList = this.isHotKeyword();
        ResponseSearchIndexData data = ResponseSearchIndexData.builder()
                .defaultKeyword(defaultKey)
                .historyKeywordList(historyKeyList)
                .hotKeywordList(hotKeyList)
                .build();
        return data;
    }

    @Override
    public MarketKeyword isDefailt() {
        MarketKeywordExample example = new MarketKeywordExample();
        example.createCriteria().andIsDefaultEqualTo(true)
                .andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords = keywordMapper.selectByExample(example);

        return marketKeywords != null && marketKeywords.size() == 1 ? marketKeywords.get(0) : null;
    }

    @Override
    public List<KeyWord> isHistoryKeyword() {
        List<KeyWord> list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        example.createCriteria().andDeletedEqualTo(false);
        List<MarketSearchHistory> marketSearchHistories = searchHistoryMapper.selectByExample(example);
        for (MarketSearchHistory history : marketSearchHistories) {
            KeyWord keyWord = new KeyWord();
            keyWord.setKeyword(history.getKeyword());
            list.add(keyWord);
        }
        return list;
    }

    @Override
    public List<MarketKeyword> isHotKeyword() {
        MarketKeywordExample example = new MarketKeywordExample();
        example.createCriteria().andIsHotEqualTo(true)
                .andDeletedEqualTo(false);

        List<MarketKeyword> marketKeywords = keywordMapper.selectByExample(example);
        return marketKeywords;
    }

    @Override
    public void clearHistory() {
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        example.createCriteria().andDeletedEqualTo(false);
        List<MarketSearchHistory> marketSearchHistories = searchHistoryMapper.selectByExample(example);
        for (MarketSearchHistory history : marketSearchHistories) {
            history.setDeleted(true);
            searchHistoryMapper.updateByPrimaryKeySelective(history);
        }
    }

    @Override
    public List<String> helperKeyword(String keyword) {
        List<String> list = new ArrayList<>();
        MarketKeywordExample example = new MarketKeywordExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andKeywordLike("%" + keyword + "%");
        List<MarketKeyword> marketKeywords = keywordMapper.selectByExample(example);
        for (MarketKeyword marketKeyword : marketKeywords) {
            list.add(marketKeyword.getKeyword());
        }

        return list;
    }
}